//
//  returnMoneyRecord.m
//  jiaKaoZhuShou
//
//  Created by 金艺社 on 2018/5/22.
//  Copyright © 2018年 金艺社. All rights reserved.
//

#import "returnMoneyRecord.h"
#import "recordCell.h"
#import "returnMonryDetial.h"
#import "historyModel.h"
@interface returnMoneyRecord ()
@property (nonatomic, strong) NSMutableArray *historyList;
@property (nonatomic, assign) NSInteger page;
@end

@implementation returnMoneyRecord

- (void)viewDidLoad {
    self.title = @"还款记录";
    CGRect frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - StatusBarAndNavigationBarHeight - TabbarSafeBottomMargin);
    self.tableView = [[UITableView alloc]initWithFrame:frame];
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        //        if (iPhoneX) {
        //            self.detialView.contentInset = UIEdgeInsetsMake(0, 0, 88, 0);
        //        }else {
        //            self.detialView.contentInset = UIEdgeInsetsMake(0, 0, 64, 0);
        //        }
        //        self.detialView.scrollIndicatorInsets = self.detialView.contentInset;
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.page = 1;
    [self addRefreshHeader];
    [self addRefreshFooter];
    [self confingureTableView];
    
    [self huankuanListRequest];
}


// MARK: - 下拉刷新
- (void)addRefreshHeader {
    [super addRefreshHeader];
}
- (void)addRefreshFooter {
    [super addRefreshFooter];
}

- (void)loadNew
{
    self.page = 1;
    [self huankuanListRequest];
}

- (void)loadMore
{
    if (!self.historyList.count) {
        [self endFooterRefresh];
        return;
    }
    self.page = self.page + 1;
    [self huankuanListRequest];
}
- (void)confingureTableView {
    // [super confingureTableView];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.separatorColor = [UIColor clearColor];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([recordCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([recordCell class])];
}

#pragma mark --- uitableViewDelegate and dataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.historyList.count == 0 ? 0 : self.historyList.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 20;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 65;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    recordCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([recordCell class])];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = [self.historyList objectAtIndex:indexPath.section];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    returnMonryDetial *VC = [returnMonryDetial new];
    historyModel *model = [self.historyList objectAtIndex:indexPath.section];
    VC.ID = model.id;
    [self.navigationController pushViewController:VC animated:YES];
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UILabel *grayView = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 20)];
    grayView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    grayView.font = [UIFont customFontOfSize:11];
    grayView.text = @"";
    grayView.textColor = [UIColor custom_color:@"#666666"];
    historyModel *model = [self.historyList objectAtIndex:section];
    grayView.text = [NSString ConvertStrToTime:model.shoudRepaymentDate];
    return grayView;
}
- (void)huankuanListRequest {
    NSDictionary *dic = @{
                          @"pager.pageNumber":@(self.page),
                          @"pager.pageSize":@10,
                          @"token":USERTOKEN
                          };
    __weak typeof(self) weakSelf = self;
    NSString *url = [NSString stringWithFormat:@"%@", [[apiManger shareApiManger] repaymentHistory]];
    [networkManger PostHttpDataWithUrlStr:url Dic:dic SuccessBlock:^(id responseObject) {
        [weakSelf endRefreshing];
        
        if ([responseObject[@"rcd"] isEqualToString:R0001]) {
            [weakSelf handleResponseData:responseObject];
        }else {
            [MBProgressHUD gk_toastShowMessage:responseObject[@"rmg"]];
        }
    } FailureBlock:^(id error) {
        [weakSelf endRefreshing];
        [MBProgressHUD gk_toastShowMessage:@"请求失败"];
    }];
}
- (void)handleResponseData:(NSDictionary *)responseObject
{
    NSMutableArray *modelArray =  [historyModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"result"]];
    if (self.page == 1) {
        [self.historyList removeAllObjects];
    }
    if (modelArray.count > 0) {
        [self.historyList addObjectsFromArray:(NSArray *)modelArray];
        NSString  *count = responseObject[@"data"][@"totalCount"];
        if (count.integerValue == self.historyList.count) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
    }else {
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
    }
    [self.tableView reloadData];
}
#pragma mark --- request
- (NSMutableArray *)historyList {
    if (!_historyList) {
        _historyList = [NSMutableArray array];
    }
    return _historyList;
}
@end
